﻿using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //this.Load += Form1_Load;

        }
        public Form1(string[] a)
        {
            InitializeComponent();
            this.Text = $"参数个数:{a.Length} ";
            foreach (string s in a) 
            {
                this.Text += s;
            }

           // InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            // Set the processing mode for the ReportViewer to Local  
            reportViewer1.ProcessingMode = ProcessingMode.Local;

            LocalReport localReport = reportViewer1.LocalReport;

            localReport.ReportPath = "Sales Order Detail.rdlc";

            DataSet dataset = new DataSet("Sales Order Detail");

            string salesOrderNumber = "SO43661";

            // Get the sales order data  
            GetSalesOrderData(salesOrderNumber, ref dataset);

            // Create a report data source for the sales order data  
            ReportDataSource dsSalesOrder = new ReportDataSource();
            dsSalesOrder.Name = "SalesOrder";
            dsSalesOrder.Value = dataset.Tables["SalesOrder"];

            localReport.DataSources.Add(dsSalesOrder);

            // Get the sales order detail data  
            GetSalesOrderDetailData(salesOrderNumber, ref dataset);

            // Create a report data source for the sales order detail   
            // data  
            ReportDataSource dsSalesOrderDetail =
                new ReportDataSource();
            dsSalesOrderDetail.Name = "SalesOrderDetail";
            dsSalesOrderDetail.Value =
                dataset.Tables["SalesOrderDetail"];

            localReport.DataSources.Add(dsSalesOrderDetail);

            // Create a report parameter for the sales order number   
            ReportParameter rpSalesOrderNumber = new ReportParameter();
            rpSalesOrderNumber.Name = "SalesOrderNumber";
            rpSalesOrderNumber.Values.Add("SO43661");

            // Set the report parameters for the report  
            localReport.SetParameters(
                new ReportParameter[] { rpSalesOrderNumber });

            // Refresh the report  
            reportViewer1.RefreshReport();
            this.reportViewer1.RefreshReport();
        }

        private void GetSalesOrderData(string salesOrderNumber,
                                       ref DataSet dsSalesOrder)
        {
            string sqlSalesOrder = "SELECT * FROM dbo.Student";
                ;

            SqlConnection connection = new
                SqlConnection("Server = 222.180.242.134,77;Database = Test2;User ID = shproject;Password = shouhang;");

            SqlCommand command =
                new SqlCommand(sqlSalesOrder, connection);

            //command.Parameters.Add(
            //    new SqlParameter("SalesOrderNumber",
            //    salesOrderNumber));

            SqlDataAdapter salesOrderAdapter = new
                SqlDataAdapter(command);

            salesOrderAdapter.Fill(dsSalesOrder, "SalesOrder");
        }

        private void GetSalesOrderDetailData(string salesOrderNumber,
                               ref DataSet dsSalesOrder)
        {
            string sqlSalesOrderDetail = "SELECT *  FROM dbo.Student";



            using (SqlConnection connection = new
                SqlConnection("Server = 222.180.242.134,77;Database = Test2;User ID = shproject;Password = shouhang;"))
            {

                SqlCommand command =
                    new SqlCommand(sqlSalesOrderDetail, connection);

                //command.Parameters.Add(
                //    new SqlParameter("SalesOrderNumber",
                //    salesOrderNumber));

                SqlDataAdapter salesOrderDetailAdapter = new
                    SqlDataAdapter(command);

                salesOrderDetailAdapter.Fill(dsSalesOrder,
                    "SalesOrderDetail");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }
    }
}
